<?php
define('SUB_DIR','/services');
session_start();
error_reporting(E_ALL ^ E_NOTICE);

require '../core/service/fanwe.service.php';

$fanwe = &FanweService::instance();
$fanwe->is_group_city = false;
$fanwe->is_session = false;
$fanwe->is_user = false;
$fanwe->is_cron = false;
$fanwe->cache_list = array();
$fanwe->initialize();

set_time_limit(3600);

if(function_exists('ini_set'))
    ini_set('max_execution_time',3600);

if(function_exists('ini_get') && function_exists('ini_set'))
{
	if(substr(PHP_OS,0,3) == 'WIN')
		ini_set('include_path',ini_get('include_path').';'.str_replace('/','\\',FANWE_ROOT.'services'));
	else
		ini_set('include_path',ini_get('include_path').':'.FANWE_ROOT.'services');
}

require "Zend/Amf/Server.php";
require "fanwe.service.php";

$server = new Zend_Amf_Server();
$server -> setClass('FanweServices');

echo $server -> handle();

function dumpEscapeString($str)
{
	if ($str === null)
    {
        return null;
    }
    else if($str == '')
    {
        return '';
    }
    else
    {
		if (PHP_VERSION >= '4.3')
        {
            return mysql_real_escape_string($str);
        }
        else
        {
            return mysql_escape_string($str);
        }
    }
}

function checkLogin($message)
{
	$allow = array("Login","Logout");

	if($message->source == "FanweServices" && !in_array($message->operation,$allow))
	{
		if(isset($_SESSION['adm_name']) && !empty($_SESSION['adm_name']))
			return true;

		preg_match("/\{\"user\":\"(?P<name>.*?)\",\"password\":\"(?P<pwd>.*?)\"\}:/i",base64_decode($message->headers->DSRemoteCredentials),$user);

		if(isset($user['name']) && isset($user['pwd']) && !empty($user['name']) && !empty($user['pwd']))
		{
			$count = DB::resultFirst("SELECT count(*) FROM ".DB::table('admin')." WHERE adm_name='".urldecode($user['name'])."' and adm_pwd='".md5(urldecode($user['pwd']))."' and status = 1");

			if(intval($count) > 0)
			{
				$_SESSION['adm_name'] = $user['name'];
				return true;
			}
			else
				return false;
		}
		else
			return false;
	}
	else
		return true;
}
?>